﻿assert(Icetip, "没有找到Icetip!");

local Icetip = _G.Icetip
local Icetip_HealthBar = CreateFrame("Frame");
Icetip.HealthBar = Icetip_HealthBar
local GameTooltipStatusBar = _G.GameTooltipStatusBar
local SM = LibStub("LibSharedMedia-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale("Icetip")

function Icetip_HealthBar:OnEnable(event, unit)
	self.db = Icetip.db["healthbar"];

	self:RegisterEvent("UNIT_HEALTH");
	self:RegisterEvent("UNIT_MAXHEALTH");

	self:SetBarPoint();
	if event == "UNIT_HEALTH" then
		self:UNIT_HEALTH(unit)
	end

	Icetip_HealthBar:ToggleHealthbar(self.db.enable)
end

function Icetip_HealthBar:UNIT_HEALTH(unit)
	if not UnitIsUnit(unit, "mouseover") then
		return
	end

	self:Update();
end

function Icetip_HealthBar:SetUnitHealthBar()
	Icetip_HealthBar:ToggleHealthbar(Icetip_HealthBar.db.enable)
end

local function HealthGradient(precent)
	local r1, g1, b1
	local r2, g2, b2
	if precent <= 0.5 then
		precent = precent * 2
		r1, g1, b1 = 1, 0, 0
		r2, g2, b2 = 1, 1, 0
	else
		precent = precent * 2 - 1
		r1, g1, b1 = 1, 1, 0
		r2, g2, b2 = 0, 1, 0
	end

	return r1 +(r2-r1)*precent, g1 + (g2-g1)*precent,  b1 +(b2-b1)*precent
end

function Icetip_HealthBar:SetBarPoint()
	if not healthbar then return end
	
	local position = self.db.position
	healthbar:SetWidth(0);
	healthbar:SetHeight(0);
	healthbar:ClearAllPoints();
	healthbar.side = position
	local powerbar = _G.Icetip_Power_Bar
	if position == "BOTTOM" then
		healthbar:SetPoint("TOPLEFT", GameTooltip, "BOTTOMLEFT", 2, 0);
		healthbar:SetPoint("TOPRIGHT", GameTooltip, "BOTTOMRIGHT", -2, 0);
		healthbar:SetHeight(self.db.size);
		healthbar:SetOrientation("HORIZONTAL");
	elseif position == "TOP" then
		if powerbar and powerbar.side == "TOP" then
			healthbar:SetPoint("BOTTOMLEFT", powerbar, "TOPLEFT");
			healthbar:SetPoint("BOTTOMRIGHT", powerbar, "TOPRIGHT");
		else
			healthbar:SetPoint("BOTTOMLEFT", GameTooltip, "TOPLEFT", 2, 0);
			healthbar:SetPoint("BOTTOMRIGHT", GameTooltip, "TOPRIGHT", 2, 0);
		end
		healthbar:SetHeight(self.db.size);
		healthbar:SetOrientation("HORIZONTAL");
	end
end

function Icetip_HealthBar:OnTooltipShow()
	if not healthbar then
		healthbar = CreateFrame("StatusBar", "Icetip_Health_Bar", GameTooltip);
		healthbar:SetStatusBarTexture(SM:Fetch("statusbar", Icetip_HealthBar.db.texture));
		healthbar:SetMinMaxValues(0, 1);
		Icetip_HealthBar:SetBarPoint()
	end
	
	if not GameTooltip:GetUnit() then
		healthbar:Hide()
		return
	end
	
	if Icetip_HealthBar.db.enable then
		healthbar:Show();
	end
	Icetip_HealthBar:Update()
end

function Icetip_HealthBar:OnTooltipHide()
	if not healthbar then return end
	healthbar:Hide();
end

function Icetip_HealthBar:Update()
	if not healthbar then return end
	
	local hpmax = UnitHealthMax("mouseover");
	local hp = UnitHealth("mouseover");
	local value;
	if hpmax == 0 then
		value = 0
	else
		value = hp/hpmax
	end
	healthbar:SetValue(value);
	healthbar:SetStatusBarColor(HealthGradient(value))
end

function Icetip_HealthBar:ToggleHealthbar(flag)
	if flag then
		GameTooltipStatusBar:Hide();
		Icetip_HealthBar:Update()
	else
		if healthbar then
			healthbar:Hide()
			GameTooltipStatusBar:Hide()
		end
		GameTooltipStatusBar:Hide()
	end
end

Icetip_HealthBar:RegisterEvent("PLAYER_LOGIN");
Icetip_HealthBar:SetScript("OnEvent", Icetip_HealthBar.OnEnable)